package com.postracker.postracker.io.txt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.postracker.postracker.io.ListOfRows;
import com.postracker.postracker.io.exception.FileException;

public class TextRowFileDecorator extends File implements ListOfRows {

	private static final long serialVersionUID = -3438201978475052998L;

	protected TextRowFileDecorator(String absolutePath) {
		super(absolutePath);
	}

	private List<String> loadAllRows() throws IOException {
		FileInputStream fstream = new FileInputStream(this);
		InputStreamReader inputStreamReader = new InputStreamReader(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
		List<String> allLines = new ArrayList<String>();
		for (String strLine = br.readLine(); strLine != null; strLine = br
				.readLine()) {
			allLines.add(strLine);
		}
		inputStreamReader.close();
		return allLines;
	}

	@Override
	public List<String> getRows() {
		try {
			return loadAllRows();
		} catch (IOException e) {
			throw new FileException(e);
		}
	}
}
